(add-to-list 'load-path
	     (expand-file-name 
	      "~/.emacs.d/packages/emms-mwolson/")
	     t)

;;; (require 'emms-playlist-mode)
;;; (require 'emms-source-file)
;;; (require 'emms-source-playlist)
;;; (require 'emms-player-simple)
;;; (require 'emms-player-mplayer)
;;; (require 'emms-info)
;;; (require 'emms-cache)
;;; (require 'emms-playing-time)
;;; (require 'emms-volume)
;;; (require 'emms-lyrics)

(require 'emms-setup)
(emms-devel)

;; (add-to-list 'exec-path "C:/Program Files/MPlayer-1.0rc2/")

;;; (setq emms-playlist-default-major-mode 'emms-playlist-mode)

;; external media players to handle mp3 and avi.
(setq emms-player-list
      '(emms-player-mplayer))

;; Show the current track each time EMMS starts to play a new one.
(add-hook 'emms-player-started-hook 'dotnil-emms-show)
(setq emms-show-format "Now Playing: %s")

(defun dotnil-emms-show (&optional insertp)
  "Describe the current EMMS track in the minibuffer.
If INSERTP is non-nil, insert the description into the current buffer instead.
This function uses `emms-show-format' to format the current track."
  (interactive "P")
  (let ((string (if emms-player-playing-p
                    (substring (format emms-show-format
				       (emms-track-description
					(emms-playlist-current-selected-track)))
			       0 fill-column)
                  "Nothing playing right now")))
    (if insertp
        (insert string)
      (message "%s" string))))

;; Turn off emms-mode-line current playing display.
(when (fboundp 'emms-mode-line)
  (emms-mode-line -1))

;; lyrics settings, don't display on minibuffer.
(when (fboundp 'emms-lyrics)
  (emms-lyrics 1))
(setq emms-lyrics-coding-system 'gbk
      emms-lyrics-display-on-minibuffer nil
      emms-lyrics-dir "~/Music/lyrics")

;; cache media data such as tracks.
(when (fboundp 'emms-cache)
  (emms-cache 1))
(setq emms-cache-file-coding-system 'utf-8)

;; as the code tells, display playing time.
(when (fboundp 'emms-playing-time)
  (emms-playing-time 1))
(setq emms-playing-time-display-format "%s ")

(setq emms-playlist-buffer-name "*EMMS*"
      emms-source-file-default-directory "~/Music/"
      emms-info-asynchronously nil
      )

;; YeWenbin's emacs extention, scroll the mode line, wrap up some space.
;; (require 'scroll-mode-line-mode)
;; (scroll-mode-line-mode 1)

;; global key-map
(global-set-key (kbd "<f9>") 'emms-playlist-mode-go)
(global-set-key (kbd "C-c e m") 'emms-playlist-mode-go)
(global-set-key (kbd "C-c e M") 'emms-show)
(global-set-key (kbd "C-c e l") 'emms-lyrics-toggle-display-on-modeline)

(global-set-key (kbd "C-c e t") 'emms-play-directory-tree)
(global-set-key (kbd "C-c e e") 'emms-play-file)
(global-set-key (kbd "C-c e d") 'emms-play-directory)
(global-set-key (kbd "C-c e f") 'emms-play-playlist)
(global-set-key (kbd "C-c e x") 'emms-start)
(global-set-key (kbd "C-c e v") 'emms-stop)
(global-set-key (kbd "C-c e n") 'emms-next)
(global-set-key (kbd "C-c e p") 'emms-previous)

;; playlist-mode-map
(define-key emms-playlist-mode-map (kbd "x") 'emms-start)
(define-key emms-playlist-mode-map (kbd "v") 'emms-stop)
(define-key emms-playlist-mode-map (kbd "t") 'emms-add-directory-tree)
(define-key emms-playlist-mode-map (kbd "d") 'emms-add-directory)
(define-key emms-playlist-mode-map (kbd "e") 'emms-add-file)
(define-key emms-playlist-mode-map (kbd "f") 'emms-add-playlist-file)

(define-key emms-playlist-mode-map (kbd "SPC") 'emms-pause)
(define-key emms-playlist-mode-map (kbd "=") 'emms-volume-raise)
(define-key emms-playlist-mode-map (kbd "-") 'emms-volume-lower)

;;; Track Show Format (for playlist buffer)

(setq emms-last-played-format-alist
      '(((emms-last-played-seconds-today) . "%a %H:%M")
	(604800                           . "%a %H:%M") ; this week
	((emms-last-played-seconds-month) . "%d")
	((emms-last-played-seconds-year)  . "%m/%d")
	(t                                . "%Y/%m/%d")))

(eval-after-load "emms"
  '(progn

     (setq xwl-emms-playlist-last-track nil)
     (setq xwl-emms-playlist-last-indent "\\")

     (defun xwl-emms-track-description-function (track)
       "Return a description of the current track."
       (let* ((name (emms-track-name track))
              (type (emms-track-type track))
              (short-name (file-name-nondirectory name))
              (play-count (or (emms-track-get track 'play-count) 0))
              (last-played (or (emms-track-get track 'last-played) '(0 0 0)))
              (empty "..."))
         (prog1
             (case (emms-track-type track)
               ((file url)
                (let* ((artist (or (emms-track-get track 'info-artist) empty))
                       (year (emms-track-get track 'info-year))
                       (playing-time (or (emms-track-get track 'info-playing-time) 0))
                       (min (/ playing-time 60))
                       (sec (% playing-time 60))
                       (album (or (emms-track-get track 'info-album) empty))
                       (tracknumber (emms-track-get track 'info-tracknumber))
                       (short-name (file-name-sans-extension
                                    (file-name-nondirectory name)))
                       (title (or (emms-track-get track 'info-title) short-name))

                       ;; last track
                       (ltrack xwl-emms-playlist-last-track)
                       (lartist (or (and ltrack (emms-track-get ltrack 'info-artist))
                                    empty))
                       (lalbum (or (and ltrack (emms-track-get ltrack 'info-album))
                                   empty))

                       (same-album-p (and (not (string= lalbum empty))
                                          (string= album lalbum))))
                  (format "%10s  %3d   %-20s%-60s%-35s%-15s%s"
                          (emms-last-played-format-date last-played)
                          play-count
                          artist

                          ;; Combine indention, tracknumber, title.
                          (concat
                           (if same-album-p ; indention by album
                               (setq xwl-emms-playlist-last-indent
                                     (concat " " xwl-emms-playlist-last-indent))
                             (setq xwl-emms-playlist-last-indent "\\")
                             "")
                           (if (and tracknumber ; tracknumber
                                    (not (zerop (string-to-number tracknumber))))
                               (format "%02d." (string-to-number tracknumber))
                             "")
                           title        ; title
                           )

                          ;; album
                          (cond ((string= album empty) empty)
                                ;; (same-album-p "  ")
                                (t (concat "<" album ">")))

                          (or year empty)
                          (if (or (> min 0)  (> sec 0))
                              (format "%02d:%02d" min sec)
                            empty))))
               ((url)
                (concat (symbol-name type) ":" name))
               (t
                (format "%-3d%s"
                        play-count
                        (concat (symbol-name type) ":" name))))
           (setq xwl-emms-playlist-last-track track))))

     (setq emms-track-description-function
           'xwl-emms-track-description-function)
     ))
